<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
<title>精准微课堂-班级-课堂</title>
<%@ include file="/common/commonLib.jsp"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/css/class-index.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/css/common-main.css'/>"/>
<script src="<c:url value='/js/class-index.js'/>" type="text/javascript" charset="utf-8"></script>
</head>
<body>
		<%@include file="/common/header.jsp"%>
		<div class="container-fluid main">
			<%@include file="common/classHeader.jsp"%>
			<div class="row class-index-con">
				<div class="col-md-9 col-lg-9">
					<div class="row">
						<div class="class-index-con-list">
							<ul>
								<li class="class-room"><a href="<c:url value='/lesson/listView?id=${param.id}'/>" style="color:#00b43c;">课堂</a></li>
								<li class="class-stu"><a href="<c:url value='/cscontrol/listView?id=${param.id}'/>">学生</a></li>
							</ul>
						</div>
					</div>
					<div class="row">
						<div class="class-index-con-content">
							<!--理论课&实验课 -->
							<div class="lab-and-theory">
								<%@include file="classLessonList.jsp"%>
							</div>
							<!--已加入小班学生列表-->
							<div class="student-smallclass">
								<%@include file="classHasInSmallClass.jsp"%>
							</div>
						</div>
					</div>
				</div>
				
				<!--小班列表 -->
				<div class="col-md-3">
					<%@include file="common/classRight.jsp"%>
				</div>
			</div>
		</div>
		
		<!--将要添加小班学生列表弹出框-->
		<div id="addstudent" style="display:none;font-size:1.6rem;">
			<%@include file="classAddSmallStudent.jsp"%>
		</div>
		
		<%@include file="/common/footer.jsp"%>
	</body>
	
<script>
var data=${data};
var lessonLab;
$(document).ready(function(){
	if (data.status != "success"){
		layer.alert(data.message);
		return;
	};
	lessonLab="THEORY";
	initPage();
	initLesson(data.data);
	$("#navCourseName").attr("href",CONTEXT_PATH + "task/list?courseId=" + data.data.classView.courseId);
	
});

//初始化课程
var initLesson=function(data){
	var lessons=data.list;
	var theoryList=$("#theoryList");
	var labList=$("#labList");
	theoryList.empty();
	labList.empty();
	var flagT = 0, flagL = 0;
	var noLesson = "<li>暂无课程</li>";
	for(var i=0;i<lessons.length;i++){
		var s = "";
		if(lessons[i].createType=="NEWADD") {
			s+="<li class=\"newClass\" onclick=\"lessonDetail("+lessons[i].id+")\">";
		} else {
			s+="<li onclick=\"lessonDetail("+lessons[i].id+")\">";
		}
		s+="<a title=\""+lessons[i].beginDate+"--"+lessons[i].endDate+"\">"+lessons[i].beginDate+"--"+lessons[i].endDate;
		if(lessons[i].status=="NOTSTART"){
			s+="<em class=\"classType\">未开始</em>";
		}else if(lessons[i].status=="INPROGRESS"){
			s+="<em class=\"classType\">进行中</em>";
		}else{
			s+="<em class=\"classType\">已结束</em>";
		}
		s+="</a></li>";
		if(lessons[i].type == "THEORY") {
			theoryList.append(s);
			flagT += 1;
		} else {
			labList.append(s);
			flagL += 1;
		}
	}
	if(flagT == 0){
		theoryList.append(noLesson);
	}
	if(flagL == 0){
		labList.append(noLesson);
	}
	initPages(data.pageNo,data.allpages);
}
var lessonDetail = function(id){
	window.location.href = CONTEXT_PATH+"lessonTaskView/tlview?lessonId="+id;
}
var getLesson=function(json){

	$.get(CONTEXT_PATH+"lesson/ajaxListView",{
		data : JSON.stringify(json)
	},function(data) {
		lessonLab=json.search.type;
		initLesson(data.data);
	});
}

var getCStudents = function(){
	var id=$("input[name='classId']").val();
	var json={
		classId:id
	};
	
	$.get(CONTEXT_PATH+"cscontrol/ajaxListData",{
		data : JSON.stringify(json)
	},function(data) {
		if(data.status=="success"){
			var searchSmallStudentList = $("#searchSmallStudentList");
			searchSmallStudentList.empty();
    		var s="";
    		var students=data.data.list;
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s+="<tr>";
    			s+="<th scope=\"row\">"+(i+1)+"</th>";
    			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			s+="<td><input type=\"checkbox\" name=\"smallStudentId\" value=\""+item.id+"\" /></td>";
    			s+="</tr>"
    		}
    		searchSmallStudentList.append(s);
		}else{
			alert(data.message);
		}
	}); 
}
var seachSmallStudentEvent = function(){
	var keywords=$("input[name='searchSmallStudent']").val();
	var classId=$("input[name='classId']").val();
	if(keywords==""){
		getCStudents();
		return;
	}
	var json={
			keywords:keywords,
			classId:classId,
			pageNo:1
	};
	$.get(CONTEXT_PATH+"cscontrol/searchListData", {
		data : JSON.stringify(json)
	} ,function(data) {
		if(data.status=="success"){
			var searchSmallStudentList = $("#searchSmallStudentList");
			searchSmallStudentList.empty();
    		var s="";
    		var students=data.data.list;
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s+="<tr>";
    			s+="<th scope=\"row\">"+(i+1)+"</th>";
    			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			s+="<td><input type=\"checkbox\" name=\"smallStudentId\" value=\""+item.id+"\" /></td>";
    			s+="</tr>"
    		}
    		searchSmallStudentList.append(s);
		}else{
			alert(data.message);
		}
	}); 
}
var batchAddSmallStudentToClass = function (){
	var chk_value =[];
	$('input[name="smallStudentId"]:checked').each(function(){ 
		chk_value.push($(this).val()); 
	});
	if(chk_value.length<=0){
		alert("请选择学生");
		return ;
	}
	var smallStudentId=JSON.stringify(chk_value);
	var classId=$("input[name='classId']").val();
	var json={
		"classId":classId,
		"studentId":chk_value
	}
	$.post(CONTEXT_PATH+"cscontrol/ajaxBatchCreateSmallClass", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			getCStudents();
			setTimeout(function(){
				layer.closeAll();
			},300);
			getSmallStudentList(classId);
		}else{
			alert("wrong");
		}
	}); 
	
}
var smallStudentDelete = function(){
	var classId=$("input[name='classId']").val();
	var studentIds =[];
	$('input[name="deleteSmallStudentId"]:checked').each(function(){ 
		studentIds.push($(this).val()); 
	});
	if(studentIds.length<=0){
		alert("请选择待删除学生");
		return ;
	}
	var json={
			ids:studentIds,
			keyId:classId
	};
	$.post(CONTEXT_PATH+"cscontrol/batchDelete", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			getSmallStudentList(classId);
		}else{
			alert(data.message);
		}
	});
}

var getStudentList = function(){
	var classId=data.data.classView.id;
	$(".student-bigclass").show();
	var json={
			classId:classId,
			pageNo:1
	};
	$.get(CONTEXT_PATH+"cscontrol/ajaxListView",{
		data : JSON.stringify(json)
	},function(data) {
		if(data.status=="success"){
			$("#className").text(data.data.classView.className+"学生列表");
			$(".class-name").text(data.data.classView.className);
			var studentList = $("#studentList");
			studentList.empty();
    		var s="";
    		var students=data.data.students;
    		
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s+="<tr>";
    			s+="<th scope=\"row\">"+(i+1)+"</th>";
    			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			s+="<td><input type=\"checkbox\" name=\"deleteStudentId\" value=\""+item.id+"\" /></td>";
    			s+="</tr>"
    		}
    		studentList.append(s);
		}else{
			alert(data.message);
		}
	}); 
}

var seachStudentEvent = function(){
	var keywords=$("input[name='searchStudent']").val();
	if(keywords==""){
		alert("请输入搜索信息");
		return;
	}
	var json={
			keywords:keywords,
			classId:data.data.classView.id
	};
	$.get(CONTEXT_PATH+"cscontrol/searchListData", {
		data : JSON.stringify(json)
	},function(data) {
		if(data.status=="success"){
			var searchStudentList = $("#searchStudentList");
			searchStudentList.empty();
    		var s="";
    		var students=data.data.list;
    		for(var i=0;i<students.length;i++){
    			var item=students[i];
    			s+="<tr>";
    			s+="<th scope=\"row\">"+(i+1)+"</th>";
    			s+="<td title=\""+item.studentCode+"\">"+item.studentCode+"</td>";
    			s+="<td title=\""+item.name+"\">"+item.name+"</td>";
    			if(item.hasJoined==1){
	    			s+="<td>已添加</td>";
    			}else{
        			s+="<td><button onclick=\"addStudent("+item.id+")\" class=\"btn btn-primary\">添加</button></td>";
    			}
    			s+="</tr>";
    		}
    		searchStudentList.append(s);
		}else{
			alert(data.message);
		}
	}); 
}

var addStudent= function(studentId){
	var json={
			classId:data.data.classView.id,
			studentId:studentId
	}
	$.post(CONTEXT_PATH+"cscontrol/ajaxCreate", {
		data : JSON.stringify(json)
	} ,function(data) {
		if(data.status=="success"){
			getStudentList();
			seachStudentEvent();
		}else{
			alert(data.message);
		}
	});
	
}

var studentDelete = function(){
	var classId=data.data.classView.id;
	var studentIds =[];
	$('input[name="deleteStudentId"]:checked').each(function(){ 
		studentIds.push($(this).val()); 
	});
	if(studentIds.length<=0){
		alert("请选择待删除学生");
		return ;
	}
	var json={
			ids:studentIds,
			keyId:classId
	};
	$.post(CONTEXT_PATH+"cscontrol/batchDelete", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			getStudentList();
		}else{
			alert(data.message);
		}
	});
}

var initPages = function(pageNo,allpages){
	if(allpages <= 1){
		$("#lessonPage").hide();
	}else {
		$("#lessonPage").show();
		var lastPageObj=$("#lessonPage .last-page");
		var nextPageObj=$("#lessonPage .next-page");
		var pageNumberObj=$("#lessonPage .page-number");
		var lastPage=pageNo-1;
		if(lastPage<=0){//设置disableed
			lastPage=1;
		}else{
			 $("#lessonPage .last-page").bind("click",{pageNo:lastPage},getPage);
		};
		var nextPage = pageNo+1;
		if(nextPage>allpages){
			nextPage=allpages;
		}else{
			 $("#lessonPage .next-page").bind("click",{pageNo:nextPage}, getPage);
		}
		pageNumberObj.text("第"+pageNo+"页/共"+allpages+"页");
	}	
}
var getPage = function(event){
	var classId=data.data.classView.id;
	var json={
			search:{
				classId:classId,
				type:lessonLab
			}
	};	
	json.pageNo=event.data.pageNo;
	getLesson(json);
}
</script>
</html>